import urllib
import urllib.request
import urllib.parse
from urllib.error import URLError, HTTPError
import http.client



##params =urllib.parse.urlencode({'page': 1})
##headers = {
##"Accept": r"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",\
##"Accept-Encoding": r"gzip, deflate",\
##"Accept-Language": r"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",\
##"Connection": r"keep-alive",\
##"Cookie":r"__gads=ID=eb153f20b5eeed72:T=1335273441:S=ALNI_MYVyMx24KbIcTioUcyMfdAr1WsGWg; __utma=208385984.1137124298.1335273440.1335354902.1335355117.5; __utmz=208385984.1335273442.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); cck_lasttime=1335354676093; cck_count=1; AJSTAT_ok_times=2; __utmb=208385984; AJSTAT_ok_pages=4; __utmc=208385984",\
##"Host": r"www.cnbeta.com",\
##"Referer": r"http://www.cnbeta.com/",\
##"User-Agent": r"Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",\
##"X-Requested-With": r"XMLHttpRequest"}
##conn = http.client.HTTPConnection('www.cnbeta.com')
##conn.request("GET", r"/backend.php","",headers)
##r1 = conn.getresponse()
##print(r1.status, r1.reason)
##print(r1.read().decode('GB2312'))


#params = urllib.parse.urlencode({'page': '1'})
params = ""
headers = {
"Accept": r"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",\
"Accept-Encoding":"gzip, deflate",\
"Accept-Language": r"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",\
"Connection": r"keep-alive",\
"Cookie":r"__gads=ID=eb153f20b5eeed72:T=1335273441:S=ALNI_MYVyMx24KbIcTioUcyMfdAr1WsGWg; __utma=208385984.1137124298.1335273440.1335355117.1335364870.6; __utmz=208385984.1335273442.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); cck_lasttime=1335354676093; cck_count=1; AJSTAT_ok_times=3; __utmb=208385984; __utmc=208385984; AJSTAT_ok_pages=1",\
"Host": r"www.cnbeta.com",\
"Referer": r"http://www.cnbeta.com/",\
"User-Agent": r"Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0"}
conn = http.client.HTTPConnection('www.cnbeta.com')
conn.request("GET", r"/backend.php")
#conn.request("GET", r"/newread.php",params,headers)
r1 = conn.getresponse()
print(r1.status, r1.reason)
print(r1.getheaders())
print(r1.read().decode('GB2312','ignore'))

conn2 = http.client.HTTPConnection('cnbeta.feedsportal.com')
conn2.request("GET", r"/c/3406/f/624776/index.rss")
r2 = conn2.getresponse()
print(r2.status, r2.reason)
print(r2.getheaders())
with open('py1.xml', mode='w', encoding='UTF-8') as a_file:
    a_file.write(r2.read().decode('UTF-8'));
    
##print(r2.read().decode('UTF-8'))

#url="http://www.cnbeta.com/newread.php"  
  
##values={"page":"1"}  
##data=urllib.parse.urlencode(values)  
##  
##theurl=url+"?"+data
##print(theurl)
###创建请求对象  
##req=urllib.request.Request(theurl)
##req.add_header("Content-Type","text/html;charset=GB2312")  
###获得服务器返回的数据
##response=urllib.request.urlopen(req)
###处理数据  
##page=response.read()
##print(page)
